package one.bugu.android.demon.util;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Picture;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.ViewGroup;
import android.webkit.WebView;


import com.juefeng.android.framework.common.util.ToastUtils;

import one.bugu.android.demon.R;
import one.bugu.android.demon.constant.Constant;
import one.bugu.android.demon.constant.HttpConstant;

/**
 * 分享图片合成工具类
 */
public class ShareImageUtils {
    /**
     * 二维码文件名称
     */
    public static final String qrCodeImageName = "/shareQrCodeImage.jpeg";

    /***
     * 分享图片保存位置
     */
    public static final String shareImagePath = "/shareImageWithQr.jpeg";


    /**
     * 单例对象
     */
    private static volatile ShareImageUtils singleton = null;

    private ShareImageUtils() {

    }

    /**
     * 获取单例对象
     *
     * @return 单例对象
     */
    public static ShareImageUtils getInstance() {
        if (singleton == null) {
            synchronized (ShareImageUtils.class) {
                if (singleton == null) {
                    singleton = new ShareImageUtils();
                }
            }
        }
        return singleton;
    }


    /**
     * 邀请链接合成二维码
     */
    public void compQRCode(Activity activity) {
        try {
            //邀请链接
            String inviteCode = PreferencesUtil.getInstance().getString(activity, Constant.INVITE_CODE);
            String userName = PreferencesUtil.getInstance().getString(activity, Constant.USER_NAMR);
            String userTel = PreferencesUtil.getInstance().getString(activity, Constant.USERTEL);
            if (TextUtils.isEmpty(userName)) {
                userName = userTel;
            }
            userName = CodeUtils.getInstance().str2Utf8(userName);
            String url = HttpConstant.INVITE_H5_URL + "?code=" + inviteCode + "&userName=" + userName;
            //背景
            Bitmap bitmap = BitmapFactory.decodeResource(activity.getResources(), R.mipmap.icon_share_image);
            int height = bitmap.getHeight();
            int width = bitmap.getWidth();
            //重新定义背景文件的大小
            int phoneWidth = ScreenUtils.getPhoneWidth(activity);
            int realHeight = phoneWidth * height / width;
            bitmap = bitmap.createScaledBitmap(bitmap, phoneWidth, realHeight, false);
            //合成二维码
            Bitmap qrImage = ZXingUtils.createQRImage(url, realHeight, realHeight);
            if (qrImage != null) {
                //合成图片
                Bitmap newBitmap = Bitmap.createBitmap(phoneWidth, realHeight, Bitmap.Config.ARGB_8888);
                Canvas cv = new Canvas(newBitmap);
                cv.drawColor(Color.WHITE);
                cv.drawBitmap(bitmap, 30, 0, null);
                cv.drawBitmap(qrImage, 0, 0, null);
                cv.save(Canvas.ALL_SAVE_FLAG);
                cv.restore();
                //保存bitmap
                String imageFile = ShareDialogUtils.getInstance().getImageFile(activity);
                ImageUtils.saveImageAndCompress(newBitmap, (imageFile + qrCodeImageName));
            } else {
                Log.e("compQRCode", "二维码合成失败...");
            }
            if (bitmap != null) {
                bitmap.recycle();
            }
            if (qrImage != null) {
                qrImage.recycle();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 原生屏幕截屏 可带dialog
     *
     * @param activity
     * @param dialog
     */
    public void compShareImage(final Activity activity, Dialog dialog) {
        //屏幕截屏
        Bitmap screenBitmap = ScreenshotUtils.saveScreenShot(activity, dialog);
        if (screenBitmap == null) {
            return;
        }
        //获取二维码
        String imageFile = ShareDialogUtils.getInstance().getImageFile(activity);
        Bitmap qrBitmap = BitmapFactory.decodeFile(imageFile + ShareImageUtils.qrCodeImageName);
        if (qrBitmap != null) {
            //合成图片
            Bitmap newBitmap = Bitmap.createBitmap(ScreenUtils.getPhoneWidth(activity),
                    screenBitmap.getHeight() + qrBitmap.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas cv = new Canvas(newBitmap);
            cv.drawColor(Color.WHITE);
            cv.drawBitmap(screenBitmap, 0, 0, null);
            cv.drawBitmap(qrBitmap, 0, screenBitmap.getHeight(), null);
            cv.save(Canvas.ALL_SAVE_FLAG);
            cv.restore();
            //保存bitmap
//            String imageres = ShareDialogUtils.getInstance().getImageFile(activity);
//            ImageUtils.saveImageAndCompress(newBitmap, (imageres + shareImagePath));
            //分享出去图片
            ShareDialogUtils.getInstance().shareBmpImage(activity, newBitmap);

        }
    }

    /**
     * H5页面截屏
     *
     * @param activity
     * @param webView
     */
    public void compShareWebImage(final Activity activity, WebView webView, ViewGroup group, boolean isFull) {
        try {
            //截取topBar
            Bitmap topBarBmp = ScreenshotUtils.saveScreen(group);
            Bitmap webBitmap = null;
            if (isFull) {
                //获取webview全部内容截图
                Picture snapShot = webView.capturePicture();
                webBitmap = Bitmap.createBitmap(snapShot.getWidth(), snapShot.getHeight(), Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(webBitmap);
                snapShot.draw(canvas);
            } else {
                webView.scrollTo(0, 0);
                webBitmap = webView.getDrawingCache();
            }
            if (webBitmap == null) {
                return;
            }
            //合成图片
            //获取二维码
            String imageFile = ShareDialogUtils.getInstance().getImageFile(activity);
            Bitmap qrBitmap = BitmapFactory.decodeFile(imageFile + ShareImageUtils.qrCodeImageName);
            if (qrBitmap != null) {
                //合成图片
                Bitmap newBitmap = Bitmap.createBitmap(ScreenUtils.getPhoneWidth(activity),
                        topBarBmp.getHeight() + webBitmap.getHeight() + qrBitmap.getHeight(), Bitmap.Config.ARGB_8888);
                Canvas cv = new Canvas(newBitmap);
                cv.drawColor(Color.WHITE);
                cv.drawBitmap(topBarBmp, 0, 0, null);
                cv.drawBitmap(webBitmap, 0, topBarBmp.getHeight(), null);
                cv.drawBitmap(qrBitmap, 0, topBarBmp.getHeight() + webBitmap.getHeight(), null);
                cv.save(Canvas.ALL_SAVE_FLAG);
                cv.restore();
                //保存bitmap
//            String imageres = ShareDialogUtils.getInstance().getImageFile(activity);
//            ImageUtils.saveImageAndCompress(newBitmap, (imageres + shareImagePath));
                //分享出去图片
                ShareDialogUtils.getInstance().shareBmpImage(activity, newBitmap);
            }
        } catch (Exception e) {
            e.printStackTrace();
            ToastUtils.custom("分享失败");
        }
    }

    /**
     * 获取分享图片
     */
    public String getShareImagePath(Activity activity) {
        String imageFile = ShareDialogUtils.getInstance().getImageFile(activity);
        return imageFile + shareImagePath;
    }
}
